<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style></style>
</head>

<body>
  <script>
    const oldobj = {
      name: 'xiaming',
      age: '20',
      colors: ['red', 'blue', 'black'],
      frind: {
        name: 'xiaohong'
      }
    }
    // const newobj1 = oldobj
    // newobj1.name = 'b'
    // console.log('oldobj', oldobj)
    // console.log('newobj1', newobj1)

    function deepClone(obj = {}) {
      if (typeof obj !== 'object' || obj == null) {
        return obj;
      }
      let result;
      if (obj instanceof Array) {
        result = [];
      } else {
        result = {};
      }

      for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
          result[key] = deepClone(obj[key]);
        }
      }

      return result;
    }

    const newobj = deepClone(oldobj);
    newobj.frind.name = 'xxx';
    console.log('oldobj', oldobj)
    console.log('newobj', newobj)
    

    

  </script>
</body>

</html>